package com.example.transvipm;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.Objetos.ObjMsj;
import com.example.dbclass.DBHelper;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.BaseColumns;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Mensaje extends Activity {

	private DBHelper dbmjs;
	TextView num;
	String Numero;
	ListView lista;
	private ImageButton v_menu;
	private Vibrator vib; 
	

	ArrayList<String> plan = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mensajes);
		v_menu = (ImageButton) findViewById(R.id.Volver_menu);
		vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		dbmjs = new DBHelper(getApplicationContext());
		lista = (ListView) findViewById(R.id.listamsj);
		num = (TextView) findViewById(R.id.numero_movil);
		Bundle Datos = getIntent().getExtras();
		Numero = Datos.getString("Numero");
		num.setText(Numero);
		//
		String[] from = new String[] { "num", "title" };
		int[] to = new int[] {R.id.NumeroM, R.id.Mensaje};
		ArrayList<ObjMsj> msj = new ArrayList<ObjMsj>();
		
		try{
			
			msj = dbmjs.ObtenerMensajes();
			
			}catch(SQLiteDatabaseLockedException e)
			{
				 Toast.makeText(Mensaje.this, "Error al Realizar la Consulta, Intente Nuevamente",
			                Toast.LENGTH_LONG).show();
			}
		
		
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		int cont = 0;
		
		
		for (ObjMsj i : msj) {
			cont++;
			
			HashMap<String, String> h1 = new HashMap<String, String>();
			h1.put(BaseColumns._ID, String.valueOf(i.getId()));
			String titulo = cont + "  "+i.getContenido();
			h1.put(from[0],titulo);
			String texto= i.getLinea();
		
			h1.put(from[1], texto);
			
			
			list.add(h1);
		
		}

		SimpleAdapter adaptador = new SimpleAdapter(this, list, R.layout.row2,
				from, to);
		lista.setAdapter(adaptador);
		
		v_menu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				vib.vibrate(300);
				onBackPressed();
			}
		});
		
		v_menu.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {

					v_menu.setImageResource(R.drawable.menu_pressed);

				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					v_menu.setImageResource(R.drawable.menu);

				}
				
				return false;
			}
		});

		
	}
	public void onBackPressed() {
		super.onBackPressed();
	};

}
